package com.market.service.impl;

import com.market.entity.*;
import com.market.mapper.OrderMapper;
import com.market.service.OrderService;
import com.market.utils.GetDistance;
import com.market.utils.LocationUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.sql.Timestamp;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderDetailImpl orderDetailImpl;

    @Resource
    private GoodsServiceImpl goodsServiceImpl;

    @Resource
    private BuyerServiceImpl buyerServiceImpl;

    @Resource
    private StoreServiceImpl storeServiceImpl;


    //新增一笔订单
    @Override
    public boolean addOrder(Order order) {
        //根据买家地址找到最近的小店
        Buyer buyer = buyerServiceImpl.queryBuyerById(order.getBuyerId());
        String address = buyer.getAddress();
        //获取买家经度纬度
        Map<String, Object> buyerLonLat = LocationUtil.getLngAndLat(address);
        //遍历小店地址计算距离最近的小店地址
        List<Store> allStore = storeServiceImpl.findAllStore();
        List<Double> list = new ArrayList<>();
        for (Store store : allStore) {
            double distance = GetDistance.getDistance((double) buyerLonLat.get("longitude"), (double) buyerLonLat.get("latitude"),
                    store.getLongitude(), store.getLatitude()
            );
            list.add(distance);
        }
        Double min = Collections.min(list);
        for (Store store : allStore) {
            double distance = GetDistance.getDistance((double) buyerLonLat.get("longitude"), (double) buyerLonLat.get("latitude"),
                    store.getLongitude(), store.getLatitude()
            );
            if (distance == min) {
                //将最近的小店地址设置为订单配送地址
                order.setStoreId(store.getId());
                break;
            }
        }
        boolean b = orderMapper.addOrder(order);
        return b;
    }

    //通过订单id查询订单
    @Override
    public Order queryOrderByOrderId(int id) {
        return orderMapper.queryOrderByOrderId(id);
    }

    //通过订单编号查询订单
    @Override
    public Order queryOrderByOrderCode(String orderCode) {
        Order order = orderMapper.queryOrderByOrderCode(orderCode);
        return order;
    }

    //通过买家id查询所有已下订单
    @Override
    public List<Order> queryAllBuyerOrders(int buyerId) {
        List<Order> orders = orderMapper.queryAllBuyerOrders(buyerId);
        return orders;
    }

    @Override
    public List<Order> queryDayOrders(String createTime) {
        List<Order> orders = orderMapper.queryDayOrders(createTime);
        return orders;
    }

    @Override
    public List<Order> queryAllOrders() {
        List<Order> orders = orderMapper.queryAllOrders();
        return orders;
    }

    @Override
    public boolean updateOrderPayTime(int id, String payTime) {
        boolean b = orderMapper.updateOrderPayTime(id, payTime);
        return b;
    }

    @Override
    public boolean updateOrderFinishTime(int id, String finishTime) {
        return orderMapper.updateOrderFinishTime(id, finishTime);
    }

    @Override
    public boolean updateOrderState(int id, int state) {
        if (state >= 1 && state <= 7) {
            //订单状态为已付款时:修改每日可销售数量和订单的支付时间
            if (state == 1) {
                // 1. 修改每日可销售数量
                Order order = orderMapper.queryOrderByOrderId(id);
                List<OrderDetail> orderDetails = orderDetailImpl.queryOrderDetailByOrderId(order.getId());
                int nowNum = 0;
                for (OrderDetail orderDetail : orderDetails) {
                    Goods goods = goodsServiceImpl.queryGoodsByGoodsId(orderDetail.getGoodsId());
                    nowNum = goods.getSellNum() - orderDetail.getBuyNum();
                    goodsServiceImpl.updateGoodsSellNums(orderDetail.getGoodsId(), nowNum);
                }
                // 2.修改支付时间为当前时间
                Date date = new Date();
                Timestamp nowTime = new Timestamp(date.getTime());
                orderMapper.updateOrderPayTime(id,nowTime.toString());
            }
            //订单状态为已确认收货时:修改订单的完成时间
            if (state==7){
                Date date = new Date();
                Timestamp nowTime = new Timestamp(date.getTime());
                orderMapper.updateOrderFinishTime(id,nowTime.toString());
            }

            return orderMapper.updateOrderState(id, state);
        } else {
            System.out.println("状态码超出范围1~7 当前:" + state);
            return false;
        }
    }

    @Override
    public boolean updateStoreId(int id, int storeId) {
        return orderMapper.updateStoreId(id, storeId);
    }

    @Override
    public boolean deleteOrder(int id) {
        return orderMapper.deleteOrder(id);
    }

    //修改实付金额
    @Override
    public boolean updateActualPay(double money,int orderId) {
        return orderMapper.updateActualPay(money,orderId)==1;
    }

    //修改应付金额
    @Override
    public boolean updateShouldPayment(double money, int orderId) {
        return orderMapper.updateShouldPayment(money,orderId)==1;
    }

}
